package com.webwork.lims.system.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.lims.common.dao.impl.SimpleDao;
import com.webwork.lims.system.dao.AuthorityDao;
import com.webwork.lims.system.dataobject.Authority;
import com.webwork.lims.system.dataobject.Role;
import com.webwork.lims.util.ColumnLimit;
import com.webwork.lims.util.DBSimpleSQL;
import com.webwork.lims.util.StrManager;

@Repository
public class AuthorityDaoImpl extends SimpleDao implements AuthorityDao {

	public AuthorityDaoImpl() {
		super();
		this.dbTable = "t_authority";
		dbColumns.clear();
		dbColumns.put("roleId", "ROLE_ID");
		dbColumns.put("menuCode", "MENU_CODE");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof Authority))
			return new ColumnLimit();
		Authority o = (Authority) object;
		ColumnLimit ret = new ColumnLimit();
		if (o.getRole() != Role.NOSET) {
			ret.add(dbColumns.get("roleId") + " = ");
		}
		if (!StrManager.isEmpty(o.getMenuCode())) {
			ret.add(dbColumns.get("menuCode") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		Authority o = (Authority) object;
		List<Object> ret = new ArrayList<Object>();
		if (o.getRole() != Role.NOSET) {
			ret.add(o.getRole().getCode());
		}
		if (!StrManager.isEmpty(o.getMenuCode())) {
			ret.add(o.getMenuCode());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		Authority o = (Authority) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getRole().getCode());
		objs.add(o.getMenuCode());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		Authority ret = new Authority();
		ret.setRole((Integer) map.get(dbColumns.get("roleId")));
		ret.setMenuCode((String) map.get(dbColumns.get("menuCode")));
		return ret;
	}
	
	@Override
	public void insert(Authority o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void delete(Authority o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("roleId") + " = ");
		limit.add(dbColumns.get("menuCode") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public List<Authority> query(Authority o, int page, int size) {
		List<Authority> ret = new ArrayList<Authority>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((Authority) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(Authority o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}
	
}
